package com.happymmall.dao.impl;

import com.happymmall.dao.BaseDao;
import com.happymmall.dao.UserDao;
import com.happymmall.entity.User;

import java.sql.*;

public class UserDaoImpl implements UserDao {


    @Override
    public User getLoginUser(Connection connection, String password, String username) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        User user = null;
        if (connection != null) {
            String sql = "select * from mmall_user where username = ? and password=?";
            Object[] params = {username, password};

            // 执行
            rs = BaseDao.execute(connection, pstm, rs, sql, params);

            // 处理结果,把查到的结果封装到user对象中
            if (rs.next()) {
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                // user.setPassword(rs.getString("password"));
                user.setEmail(rs.getString("email"));
                user.setPhone(rs.getString("phone"));
                // user.setQuestion(rs.getString("question"));
                //  user.setAnswer(rs.getString("answer"));
                user.setRole(rs.getInt("role"));
                user.setCreateTime(rs.getTimestamp("create_time"));
                user.setUpdateTime(rs.getTimestamp("update_time"));

            }

            // 关闭资源,因为在这里没有关闭连接，所以不需要关闭资源
            BaseDao.closeResource(null, pstm, rs);
        }
        return user;

    }


    @Override
    public int registerUser(Connection connection, User user) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        int result = 0;

        if (connection != null) {
            user.setCreateTime(new java.sql.Timestamp(System.currentTimeMillis())); // 调用 set 方法设置“创建时间”为当前时间
            user.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis())); // 调用 set 方法设置“更新时间”为当前时间
            String sql = "insert into mmall_user(username, password, email, phone, question, answer, role, create_time, update_time) values(?, ?, ?, ?, ?, ?, ?, ?, ?)";
            Object[] params = {user.getUsername(), user.getPassword(), user.getEmail(), user.getPhone(), user.getQuestion(),
                    user.getAnswer(), 1, user.getCreateTime(), user.getUpdateTime()};

            // 执行
            result = BaseDao.executeUpdate(connection, sql, params, pstm);

            BaseDao.closeResource(null, pstm, rs);
        }
        return result;

    }

    @Override
    public boolean checkvalid(Connection connection, String str, String type) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        boolean isValid = false;

        if (connection != null) {
            //动态拼接
            String sql = "select * from mmall_user where " + type + "=?";
            Object[] params = {str};

            // 执行
            rs = BaseDao.execute(connection, pstm, rs, sql, params);


            // 先判断结果集是否有数据，再关闭资源
            if (rs != null) {
                isValid = rs.next(); // 有数据则返回true
            }

            // 关闭资源（此时已经获取了结果，关闭不会影响）
            BaseDao.closeResource(null, pstm, rs);
        }
        return isValid;//布尔类型
    }

    @Override
    public User get_user_info(Connection connection, int id) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        User user = null;
        if (connection != null) {
            String sql = "select * from mmall_user where id = ?";
            Object[] params = {id};

            // 执行
            rs = BaseDao.execute(connection, pstm, rs, sql, params);

            // 处理结果,把查到的结果封装到user对象中
            if (rs.next()) {
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                // user.setPassword(rs.getString("password"));
                user.setEmail(rs.getString("email"));
                user.setPhone(rs.getString("phone"));
                // user.setQuestion(rs.getString("question"));
                //  user.setAnswer(rs.getString("answer"));
                user.setRole(rs.getInt("role"));
                user.setCreateTime(rs.getTimestamp("create_time"));
                user.setUpdateTime(rs.getTimestamp("update_time"));

            }

            BaseDao.closeResource(null, pstm, rs);
        }
        return user;


    }

    @Override
    public String forget_get_question(Connection connection, String username) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        String question = null;
        if (connection != null) {
            String sql = "select question from mmall_user where username = ?";
            Object[] params = {username};

            // 执行
            rs = BaseDao.execute(connection, pstm, rs, sql, params);
            if (rs.next())
                question = rs.getString("question");
            BaseDao.closeResource(null, pstm, rs);
        }

        return question;
    }

    @Override
    public boolean forget_check_answer(Connection connection, String username, String question, String answer) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        boolean isCorrect = false;
        if (connection != null) {
            String sql = "select * from mmall_user where username = ? and question=? and answer=?";
            Object[] params = {username, question, answer};

            // 执行
            rs = BaseDao.execute(connection, pstm, rs, sql, params);
            isCorrect = rs.next();

            BaseDao.closeResource(null, pstm, rs);
        }
        return isCorrect;


    }

    @Override
    public int forget_reset_password(Connection connection, String passwordNew, String username) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        int row = 0;
        User user = new User();

        if (connection != null) {
            user.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
            String sql = "update mmall_user set password=?,update_time=? where username=?";
            Object[] params = {passwordNew, user.getUpdateTime(), username};
            //执行
            row = BaseDao.executeUpdate(connection, sql, params, pstm);
            BaseDao.closeResource(null, pstm, rs);

        }
        return row;
    }

    @Override
    public int reset_password(Connection connection, Integer id, String passwordOld, String passwordNew) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        int row = 0;
        User user = new User();
        if (connection != null) {
            user.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
            String sql = "update mmall_user set password=?,update_time=? where id=? and password=?";
            Object[] params = {passwordNew, user.getUpdateTime(), id, passwordOld};
            //执行
            row = BaseDao.executeUpdate(connection, sql, params, pstm);
            BaseDao.closeResource(null, pstm, rs);

        }
        return row;
    }

    @Override
    public int update_information(Connection connection, String email, String phone, String question, String answer, Integer id) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        int row = 0;
        User user = new User();
        if (connection != null) {
            user.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
            String sql = "update mmall_user set email=?,phone=?,question=?,answer=?,update_time=? where id=?";
            Object[] params = {email, phone, question, answer, user.getUpdateTime(), id};
            //执行
            row = BaseDao.executeUpdate(connection, sql, params, pstm);
            BaseDao.closeResource(null, pstm, rs);

        }
        return row;
    }


}
